<?php
class Library_Model_LienHeMapper extends Library_Model_DataMapperAbstract
{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchLienHeById($id)
	{
		$db = $this->getDb();
		$selectLienhe = $db->select()
								->from('tbl_lienhe')
								->where('MaLienHe = ?',$id);
		$lienhe = $db->fetchRow($selectLienhe);
	
		return $this->_populate($lienhe);
	
	}
	public function lienheList()
	{
		$db = $this->getDb();
		$selectLienhe = $db->select()
		->from('tbl_lienhe');
		$lienhes = $db->fetchAll($selectLienhe);
	
		$lienheObjArray = array();
		foreach($lienhes as $lienhe){
			$lienheObjArray[] = $this->_populate($lienhe);
		}
	
		return $lienheObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_LienHe();
		$obj->setId($data['MaLienHe']);
		$obj->setHovaten($data['HoVaTen']);
		$obj->setDiachi($data['DiaChi']);
		$obj->setEmail($data['Email']);
		$obj->setLinhvuc($data['LinhVuc']);
		$obj->setTieude($data['TieuDe']);
		$obj->setNoidung($data['NoiDung']);
		
		return $obj;		
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data =array(
				'MaLienHe' => $obj->getId(),
				'HoVaTen' => $obj->getHovaten(),
				'DiaChi'	=>$obj->getDiachi(),
				'Email'		=>$obj->getEmail(),
				'LinhVuc'		=>$obj->getLinhvuc(),
				'TieuDe'		=>$obj->getTieude(),
				'NoiDung' =>$obj->getNoidung()
				);
		$db->insert('tbl_lienhe',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data =array(
				'MaLienHe' => $obj->getId(),
				'HoVaTen' => $obj->getHovaten(),
				'DiaChi'	=>$obj->getDiachi(),
				'Email'		=>$obj->getEmail(),
				'LinhVuc'		=>$obj->getLinhvuc(),
				'TieuDe'		=>$obj->getTieude(),
				'NoiDung' =>$obj->getNoidung()
				);
		$where = $db->quoteInto('MaLienHe = ?', $obj->getId());
		$db->update('tbl_lienhe',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaLienHe = ?',$id);
		$db->delete('tbl_lienhe',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	}